<template>
  <div class="hello">
    <van-divider>验证码登录</van-divider>

    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          { pattern: /^1[356789]\d{9}$/, message: '手机号格式错误' },
        ]"
      />
      <van-field
        v-model="sms"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
        :rules="[{ required: true, message: '请填写验证码' }]"
      >
        <template #button>
          <van-button
            size="small"
            type="primary"
            :disabled="isClick"
            @click.stop="sendCode()"
            >{{ time }}</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div >
    </van-form>
    <div class="content_btn">
      <van-button round block type="info" @click="register()" class="btn"
        >去注册</van-button
      >
      <br />
      <van-button round block type="info" @click="login()" class="btn"
        >使用密码登录</van-button
      >
    </div>
  </div>
</template>

<script>
import url from "../tools/url"
import { Toast } from "vant";
export default {
  name: "Code",
  data() {
    return {
      phone: "",
      sms: "",
      isClick: false,
      time: "发送验证码",
    };
  },
  methods: {
    login(){
       this.$router.push("/Login")
    },
    register(){
  this.$router.push("/Register")
    },
    onSubmit() {
     var str=`phone=${this.phone}&code=${this.sms}`;
      this.axios.post(url.LOGIN,str)
      .then(res=>{
        console.log(res)
        if(res.data.code==200){
          sessionStorage.setItem("token",res.data.data.token);
          this.$store.commit("sava",res.data.data)
          this.$router.push("/");
        console.log(res.data.massage);
        }else{
          console.log(res.data.massage);
        }
      })

    },
    sendCode() {
      if (/^1[356789]\d{9}$/.test(this.phone)) {
        console.log("发送验证码");
        this.isClick = true;
        var t = 60;
        var times = setInterval(() => {
          t--;
          this.time = t + "秒";
          if (t < 1) {
            this.time = "重新发送";
            this.isClick = false;
            clearInterval(times);
          }
        }, 1000);

        this.axios
          .get(url.GETCODE+"?phone=" + this.phone)
          .then((res) => {
            console.log(res);

            if (res.data.code == 200) {
              Toast(res.data.message);
              this.sms = res.data.data;
            } else {
              Toast(res.data.message);
            }
          });
      } else {
        Toast("手机号格式有误");
      }
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.btn {
  width: 40%;
  background-color: rosybrown;
}
.content_btn{
  display: flex;
  justify-content:space-around ;
}
</style>
